Skip to content

Conversation

@mohammedahmed18
Copy link
Contributor

@mohammedahmed18 mohammedahmed18 commented Jul 23, 2025

User description

How I tested it:

created a fork repo and raised a pull request in the base repo, wrote a github action to save the events.json file as an artifact

image

for some odd reason pull_request.fork is giving false (it was working before maybe they changed it IDK), the pull_request.head.repo.fork though is returning true, so we can use that.

image

PR Type

Bug fix


Description

  • Correct fork detection in is_repo_a_fork

  • Use pull_request.head.repo.fork property


File Walkthrough

Relevant files
Bug fix
env_utils.py
Update fork detection logic                                                           

codeflash/code_utils/env_utils.py

  • Updated is_repo_a_fork() logic
  • Replaced repository.fork check
  • Now reads pull_request.head.repo.fork
+1/-1     

@github-actions
Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Event Handling

The new logic only checks pull_request.head.repo.fork and removes the previous
repository.fork check. This may break fork detection for non-PR events (e.g., push).
Consider combining both checks or providing a clear fallback.

return bool(event.get("pull_request", {}).get("head", {}).get("repo", {}).get("fork", False))

@github-actions
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Add repository fallback

Add a fallback for non-pull_request events by checking repository.fork when the
pull_request context is absent. This ensures is_repo_a_fork also works for push and
other GitHub event types.

codeflash/code_utils/env_utils.py [114]

-return bool(event.get("pull_request", {}).get("head", {}).get("repo", {}).get("fork", False))
+return bool(
+    event.get("pull_request", {}).get("head", {}).get("repo", {}).get("fork")
+    or event.get("repository", {}).get("fork", False)
+)
Suggestion importance[1-10]: 8

__

Why: The suggestion restores fallback to repository.fork, improving fork detection for non-pull_request events and enhancing the function’s correctness.

Medium
Support pull_request_target

Include support for pull_request_target events by checking its head.repo.fork field.
This covers workflows triggered with pull_request_target.

codeflash/code_utils/env_utils.py [114]

-return bool(event.get("pull_request", {}).get("head", {}).get("repo", {}).get("fork", False))
+return bool(
+    event.get("pull_request", {}).get("head", {}).get("repo", {}).get("fork")
+    or event.get("pull_request_target", {}).get("head", {}).get("repo", {}).get("fork", False)
+)
Suggestion importance[1-10]: 6

__

Why: Adding support for pull_request_target events improves coverage for workflows triggered with that event but still lacks broader fallbacks.

Low

codeflash-ai bot added a commit that referenced this pull request Jul 23, 2025
…-a-fork`)

Here's how you can optimize the provided program for speed.

### Optimizations

1. **Avoid pathlib when not necessary.** `open()` is marginally faster and incurs less overhead than using `Path(...).open()`.
2. **Directly use `os.environ` instead of `os.getenv` for a (very minor) speedup, falling back to `{}` if not set.**
3. **Minimize nested `.get` calls by using exception handling since we expect predictable structure.**

Here’s the faster version.



**Notes:**
- The return value is exactly the same as before.
- All function/method names and signatures are unchanged.
- Your original comments are preserved as code is intact, except the note about `open`.
- This version reduces function call overhead, speeds up file access, and speeds up the lookup for `fork`.

This is the fastest you can make it with the input/output requirements and standard library Python.
@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Jul 23, 2025

⚡️ Codeflash found optimizations for this PR

📄 14% (0.14x) speedup for is_repo_a_fork in codeflash/code_utils/env_utils.py

⏱️ Runtime : 66.0 microseconds 57.8 microseconds (best of 77 runs)

I created a new dependent PR with the suggested changes. Please review:

If you approve, it will be merged into this PR (branch fix/is-repo-a-fork).


def is_repo_a_fork() -> bool:
event = get_cached_gh_event_data()
return bool(event.get("repository", {}).get("fork", False))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this must be to check the Repository on which the PR is created to (destination) and not from the source.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah looks like this, although this was actually giving true before for forked repository, maybe it was a bug on their end

@mohammedahmed18 mohammedahmed18 merged commit d15203e into main Jul 23, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants